<?
/****************************************************************************************
* Project: OpenNeighbourhood
* Date: 7.12.2006
* Author: Juhapekka Piiroinen / Isa-Maria Ojanen
* Description:
* 	This script will generate xml-document which is formatted for small screens
*	Contains also small php-scripts to convert the size of the image to other sizes
*	Reads mime information from jpg files and thumbnail.
*
* Todo:
*	Implement thumbnail generation if picture doesn't contain integrated thumbnail
*	Modify crop function so that it will crop the image from the middle not from top
*
* GNU/GPL
** (C) 2006 Juhapekka Piiroinen / Isa-Maria Ojanen
*****************************************************************************************/
?>
<?
function thumbJPG($file,$thumbfile,$img_height,$waterMark) {
  if ($file!="thumb") {
  if (!file_exists($thumbfile)) {
  $type = "jpg";
  $img_temp = @imagecreatefromjpeg($file);
  if (!$img_temp) {
	$img_temp = @imagecreatefrompng($file);
	$type = "png";
  }
  if (!$img_temp) {
	$img_temp = @imagecreatefromgif($file);
	$type = "gif";
  } 
  if (!$img_temp) {
	//echo "Ei kuvaa saatavana!";
	return;
  }

  $black = @imagecolorallocate ($img_temp, 0, 0, 0);
  $white = @imagecolorallocate ($img_temp, 255, 255, 255);
      
  $font = 2;

  $img_width=imagesx($img_temp)/imagesy($img_temp)*$img_height;
  if ($type=="gif" || $type == "png")
	$img_width = 100;
  $img_thumb=imagecreatetruecolor($img_width,$img_height);


  imagecopyresampled($img_thumb,
  $img_temp,0,0,0,0,$img_width,
  $img_height,
  imagesx ($img_temp),
  imagesy($img_temp));

  $originx = imagesx($img_thumb) - 100;
  $originy = imagesy($img_thumb) - 15;

  @imagestring ($img_thumb, $font, $originx + 10, $originy,
             $waterMark, $black);
  @imagestring ($img_thumb, $font, $originx + 11, $originy - 1,
             $waterMark, $white);
  //if ($type="jpg")
   imagejpeg($img_thumb,$thumbfile);  // write contents to file
  //if ($type="png")   
  // imagepng($img_thumb,$thumbfile); 
  //if ($type="gif")  
  // imagejpeg($img_thumb,$thumbfile);        
  imagedestroy ($img_thumb);
}
}
}


//$pathinfo = pathinfo('/dir/test.tar.gz');
//echo 'Extension: '.$pathinfo['extension'];
function du($location) {
   if (!$location or !is_dir($location)) {
     return 0;
   }

   $total = 0;

   $all = opendir($location);
   while ($file = readdir($all)) {
     if (is_dir($location.'/'.$file) and $file <> ".." and $file <> ".") {
         $total += du($location.'/'.$file);
         unset($file);
     }
     elseif (!is_dir($location.'/'.$file)) {
         $stats = stat($location.'/'.$file);
         $total += $stats['size'];
         unset($file);
     }
   }
   closedir($all);
   unset($all);
   return $total;
}

function ls ($curpath) {
   echo "<pictures>\r\n";
   $curpath = str_replace(".","",$curpath);
   if ((substr($curpath,0,1)=="/")) {
	$curpath = substr($curpath,1);
   }
   $dir = dir($curpath);

   while ($file = $dir->read()) {
	
       if($file != "." && $file != ".." && $file != "thumb") {
           $path = $curpath;
	   if (substr($curpath,-1,1)!="/")
       	   	$path = $curpath . "/";
          $full_filename = $curpath . "/" . $file;
	$pathinfo = pathinfo($full_filename);
	$imagetype = "";
	if ($pathinfo['extension']=="JPG" || $pathinfo['extension']=="jpg")
                      $imagetype="jpg";
              if ($pathinfo['extension']=="GIF" || $pathinfo['extension']=="gif")
                      $imagetype="gif";
              if ($pathinfo['extension']=="PNG" || $pathinfo['extension']=="png")
                      $imagetype="png";


           if (is_dir($path.$file)) {
               // ls($curpath.$file."/");
		?>
<path>
	<name><?=$file?></name>
</path>
<?
             } 
	     else if ($pathinfo['extension']=="pdf" || $pathinfo['extension']=="PDF") {
	     	$stats = stat($path."/".$file);
		$size = $stats['size'];
	                              ?><td><a href="<?=$path.$file?>"><img border="0" src="pdf_icon.gif" /><br /><?=$file?></a><br /><?=floor($size/1024)?>KB</td><?		                            
	}else if ($imagetype="jpg" || $imagetype="gif" || $imagetype="png") {						                      
		$imagetype = "";
//		if ($pathinfo['extension']=="JPG" || $pathinfo['extension']=="jpg")
//			$imagetype="jpg";
//		if ($pathinfo['extension']=="GIF" || $pathinfo['extension']=="gif")
//			$imagetype="gif";
//		if ($pathinfo['extension']=="PNG" || $pathinfo['extension']=="png")
//			$imagetype="png";
		
//		if ($imagetype="jpg" || $imagetype="gif" || $imagetype="png") {
	//		thumbJPG($full_filename,$curpath ."/thumb/" . $file,100,"");
		// Doesn't show the first file in the folder
		if ($pic_page<$pic_counter || ($pic_page==0 && $pic_page==$pic_counter)) {
		        $stats = stat($full_filename);
			$size = $stats['size'];
				 
		?>
<picture>
<?
$exif = exif_read_data($full_filename);
if ($exif!=false) {
?>
<?
	foreach ($exif as $key => $section) {
		//foreach ($section as $name => $val) {
		if ($key!="ComponentsConfiguration" && $key!="MakerNote" && $key!="FileSource") {
			if (trim($section)!="Array") {
				echo "\t\t<" . trim($key) . ">";
				echo trim($section);
				echo "</" . trim($key) . ">\r\n";
			} else {
				echo "\t\t<" . trim($key) . ">\r\n";
				foreach ($section as $name => $val) {
					echo "\t\t\t<" . trim($name) . ">";
					echo trim($val);
					echo "</" . trim($name) . ">\r\n";
				}
				echo "\t\t</" . trim($key) . ">\r\n";
			}
		}
	}
?>
<?
}
?>
</picture>
<?
		}
		
		
//		}
// $file<br>");
             }
 
       }

    
   }

   $dir->close();
   echo "</pictures>\r\n";
   return;
}
header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\r\n";
echo "<?xml-stylesheet type=\"text/xsl\" href=\"style.xsl\"?>\r\n";
ls('./files');
?>
